package com.snail.billing.net;

import org.json.JSONException;
import org.json.JSONObject;

import android.content.Context;
import android.text.TextUtils;
import android.widget.Toast;

import com.android.net.HttpApp;
import com.android.net.HttpResult;
import com.android.net.HttpSession;
import com.android.net.OnHttpResultListener;
import com.android.util.ResUtil;
import com.snail.billing.DataCache;
import com.snail.billing.Resource;
import com.snail.billing.json.JsonBase;
import com.snail.billing.json.JsonGeneralLogin;

public class BillingHttpApp implements OnHttpResultListener {

	private Context context;
	private RequestHttpSesssionListener requestHttpSesssionListener;

	private HttpApp httpApp;
	private OnHttpResultListener onHttpResultListener;

	private HttpSession loginHttpSession;
	private HttpSession requestHttpSession;

	public BillingHttpApp(Context context, RequestHttpSesssionListener requestHttpSesssionListener) {
		this.context = context;
		this.requestHttpSesssionListener = requestHttpSesssionListener;
	}

	public void setOnHttpResultListener(OnHttpResultListener onHttpResultListener) {
		this.onHttpResultListener = onHttpResultListener;
	}

	private Context getContext() {
		return context;
	}

	public void request() {
		if (httpApp == null) {
			httpApp = new HttpApp(context);
			httpApp.setOnHttpResultListener(this);
		}
		if (DataCache.getInstance().importParams.sessionId == null) {
			loginHttpSession = requestHttpSesssionListener.getLoginHttpSession();
			httpApp.setDialogAutoDismiss(false);
			httpApp.request(loginHttpSession);
		} else {
			requestHttpSession = requestHttpSesssionListener.getRequestHttpSesssion();
			httpApp.setDialogAutoDismiss(false);
			httpApp.request(requestHttpSession);
		}
	}

	@Override
	public void onHttpResult(HttpResult httpResult) {
		if (httpResult.isSucceed()) {
			HttpSession httpSession = httpResult.getHttpSession();
			if (httpSession.equals(loginHttpSession)) {
				JsonGeneralLogin jsonGeneralLogin = new JsonGeneralLogin((String) httpSession.getResponseData());
				if (jsonGeneralLogin.getCode() == 1) {
					if (!TextUtils.isEmpty(jsonGeneralLogin.getSessionId())) {
						DataCache.getInstance().importParams.sessionId = jsonGeneralLogin.getSessionId();
						DataCache.getInstance().importParams.aid = jsonGeneralLogin.getAid();
						requestHttpSession = requestHttpSesssionListener.getRequestHttpSesssion();
						httpApp.setDialogAutoDismiss(false);
						httpApp.request(requestHttpSession);
					} else {
						httpApp.dialogDismiss();
						Toast.makeText(getContext(), ResUtil.getString(Resource.string.snailbilling_login_fail), Toast.LENGTH_SHORT).show();
					}
				} else {
					httpApp.dialogDismiss();
					Toast.makeText(getContext(), jsonGeneralLogin.getMessage(), Toast.LENGTH_SHORT).show();
				}
			} else if (httpSession.equals(requestHttpSession)) {
				JsonData jsonData = new JsonData((String) httpSession.getResponseData());
				DataCache.getInstance().importParams.sessionId = jsonData.getSessionId();
				if (jsonData.getCode() == 13126) {
					loginHttpSession = requestHttpSesssionListener.getLoginHttpSession();
					httpApp.setDialogAutoDismiss(false);
					httpApp.request(loginHttpSession);
				} else {
					httpApp.dialogDismiss();
					if (onHttpResultListener != null) {
						onHttpResultListener.onHttpResult(httpResult);
					}
				}
			}
		}
	}

	public static interface RequestHttpSesssionListener {

		HttpSession getLoginHttpSession();

		HttpSession getRequestHttpSesssion();

	}

	public static class JsonData extends JsonBase {

		private String sessionId;

		public JsonData(String jsonString) {
			super(jsonString);
			try {
				JSONObject json = new JSONObject(jsonString);
				if (json.has("sessionId")) {
					sessionId = json.getString("sessionId");
				}
			} catch (JSONException e) {
				e.printStackTrace();
			}
		}

		public String getSessionId() {
			return sessionId;
		}

	}
}
